/*
 * OptionsView.java
 *
 * @author Michael Hansen
 * @author Lukas Thuernagel
 * @author Soeren Warszus
 */

package risk.options;

import javax.swing.*;
import java.awt.*;
import risk.options.*;
import java.util.*;
import risk.dice.ImageComponent;

/**
 * Grafische Repraesentation des Startbildschirms (Einstellungen vor Spielstart)
 * 
 */
public class OptionsView extends JFrame {
    /**
     * Das zugehoerige OptionsModel
     */
    private OptionsModel model;
    
    /**
     * Das zugehoerige OptionsController
     */
    private OptionsController controller;
    
    /**
     * Liste der erzeugten JComponents
     */
    private Map<String, JComponent> componentList = new HashMap<String, JComponent>();
    
    /**
     * Variable zum Ueberpruefen gleicher Spielernamen
     */
    public boolean sameName = false;
    
    /**
     * Erzeugt ein neuen OptionsView.
     *
     * Der View stellt die grafische Repraesentation des Einstellungsfensters
     * vor dem Spielbeginn dar.
     */
    public OptionsView(OptionsModel model) {
        super("Risiko - Startmenue");
        
        this.model = model;
        this.model.registerView(this); 
        // Controller anlegen
        this.controller = new OptionsController(this.model, this);
        
        // Werte fuer Combo-Boxen
        String[] colors = {"Rot", "Blau", "Gruen", "Gelb", "Cyan", "Grau"};
        String[] maps   = model.getMapNames();

        // benoetigte Labels anlegen
        JLabel labelMapList     = new JLabel("Kartenauswahl");
        JLabel labelPlayer      = new JLabel("Spieler");
        
        // Layout-Manager definieren
        GridBagLayout gbl      = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        int columnWidths[] = {200, 360};
        int rowHeights[] = {20, 40, 20, 200, 20};
        
        gbl.columnWidths = columnWidths;
        gbl.rowHeights = rowHeights;
        this.setLayout(gbl);
        
        // Label Kartenauswahl einbinden
        gbc.gridx = 0;  
	gbc.gridy = 0;  
        gbc.anchor = GridBagConstraints.WEST;
	gbl.setConstraints(labelMapList, gbc);
        this.getContentPane().add(labelMapList);
        
        // Combo-Box links einbinden
        gbc.gridx = 0;  
	gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        JComboBox mapList = new JComboBox(maps);
        mapList.setActionCommand("selectedMap");
        mapList.addActionListener(this.controller);
        gbl.setConstraints(mapList, gbc);
        this.getContentPane().add(mapList);
         
        // Label Spieler rechts einbinden
        gbc.gridx = 1;  
	gbc.gridy = 0;  
        gbc.anchor = GridBagConstraints.WEST;
	gbl.setConstraints(labelPlayer, gbc);
        this.getContentPane().add(labelPlayer);
        
        
        // Spieler-Panel
        JPanel rightPane = new JPanel();
        GridBagLayout playerGbl      = new GridBagLayout();
        GridBagConstraints playerGbc = new GridBagConstraints();
        int playerColumnWidths[] = {30, 110, 110};
        int playerRowHeights[]   = {30, 30, 30, 30, 30, 30};
        playerGbl.columnWidths = playerColumnWidths;
        playerGbl.rowHeights = playerRowHeights;
        
        rightPane.setLayout(playerGbl);

        // Spieler 
        addComponent(rightPane, playerGbl, playerGbc, "player1", 
                System.getProperty("user.name"), colors, 0, true);
        addComponent(rightPane, playerGbl, playerGbc, "player2", 
                "Spieler 2", colors, 1, true);
        addComponent(rightPane, playerGbl, playerGbc, "player3", 
                "Spieler 3", colors, 2, false);
        addComponent(rightPane, playerGbl, playerGbc, "player4", 
                "Spieler 4", colors, 3, false);
        addComponent(rightPane, playerGbl, playerGbc, "player5", 
                "Spieler 5", colors, 4, false);
        addComponent(rightPane, playerGbl, playerGbc, "player6", 
                "Spieler 6", colors, 5, false);

        gbc.gridx = 1;  
	gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.gridheight = 3;
        gbl.setConstraints(rightPane, gbc);
        this.getContentPane().add(rightPane);
        
        
         
        // Button zum Spiel starten
        JButton startButton = new JButton("Spiel starten");
        startButton.setActionCommand("startGame");
        startButton.addActionListener(this.controller);
        
        gbc.gridx = 0;  
	gbc.gridy = 4;
        gbc.gridwidth = 2;
        gbc.insets = new Insets(10, 0, 5, 5);
        gbc.anchor = GridBagConstraints.EAST;
        gbl.setConstraints(startButton, gbc);

        this.getContentPane().add(startButton);


        JFrame.setDefaultLookAndFeelDecorated(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        
        this.pack();
        
        this.setSize(600, 400);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((d.width - this.getSize().width) / 2, 
                         (d.height - this.getSize().height) / 2);
        this.setVisible(true);
        
    }
    
    private void addComponent(JPanel pane, GridBagLayout gbl, GridBagConstraints gbc, 
            String actionCommand, String playerName, String[] colors, 
            int selectedColor, boolean enabled) 
    {
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(5, 5, 5, 5);
        JCheckBox checkbox = new JCheckBox();
        checkbox.setSelected(enabled);  
        checkbox.setActionCommand(actionCommand);
        checkbox.addActionListener(this.controller);
        this.componentList.put(actionCommand + "Checkbox", checkbox);
        gbl.setConstraints(checkbox, gbc);
        pane.add(checkbox);
        
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        JTextField textfield = new JTextField(playerName, 30);
        textfield.setSize(100, 30);
        textfield.setEditable(enabled);
        this.componentList.put(actionCommand + "Textfield", textfield);
        gbl.setConstraints(textfield, gbc);
        pane.add(textfield);
        
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        JComboBox combobox = new JComboBox(colors);
        combobox.setSelectedIndex(selectedColor);
        combobox.setSize(100, 20);
        combobox.setEnabled(enabled);
        combobox.setEditable(false);
        combobox.setMaximumRowCount(4);
        this.componentList.put(actionCommand + "Combobox", combobox);
        gbl.setConstraints(combobox, gbc);
        pane.add(combobox);
        
        gbc.gridwidth = 1;
    }
    
    
    
    public void enablePlayer(int player)  {
        switch(player) {
            case 1: 
                if (((JCheckBox) this.componentList.get("player1Checkbox")).isSelected()) {
                    ((JTextField) this.componentList.get("player1Textfield")).setEditable(true);
                    ((JComboBox)  this.componentList.get("player1Combobox")).setEnabled(true);
                } else {
                    ((JTextField) this.componentList.get("player1Textfield")).setEditable(false);
                    ((JComboBox)  this.componentList.get("player1Combobox")).setEnabled(false);
                }
                break;
            case 2: 
                if (((JCheckBox) this.componentList.get("player2Checkbox")).isSelected()) {
                    ((JTextField) this.componentList.get("player2Textfield")).setEditable(true);
                    ((JComboBox)  this.componentList.get("player2Combobox")).setEnabled(true);
                } else {
                    ((JTextField) this.componentList.get("player2Textfield")).setEditable(false);
                    ((JComboBox)  this.componentList.get("player2Combobox")).setEnabled(false);
                }
                break;
            case 3: 
                if (((JCheckBox) this.componentList.get("player3Checkbox")).isSelected()) {
                    ((JTextField) this.componentList.get("player3Textfield")).setEditable(true);
                    ((JComboBox)  this.componentList.get("player3Combobox")).setEnabled(true);
                } else {    
                    ((JTextField) this.componentList.get("player3Textfield")).setEditable(false);
                    ((JComboBox)  this.componentList.get("player3Combobox")).setEnabled(false);
                }    
                break;
            case 4: 
                if (((JCheckBox) this.componentList.get("player4Checkbox")).isSelected()) {
                    ((JTextField) this.componentList.get("player4Textfield")).setEditable(true);
                    ((JComboBox)  this.componentList.get("player4Combobox")).setEnabled(true);
                } else {    
                    ((JTextField) this.componentList.get("player4Textfield")).setEditable(false);
                    ((JComboBox)  this.componentList.get("player4Combobox")).setEnabled(false);
                }  
                break;
            case 5: 
                if (((JCheckBox) this.componentList.get("player5Checkbox")).isSelected()) {
                    ((JTextField) this.componentList.get("player5Textfield")).setEditable(true);
                    ((JComboBox)  this.componentList.get("player5Combobox")).setEnabled(true);
                } else {    
                    ((JTextField) this.componentList.get("player5Textfield")).setEditable(false);
                    ((JComboBox)  this.componentList.get("player5Combobox")).setEnabled(false);
                }  
                break;
            case 6: 
                if (((JCheckBox) this.componentList.get("player6Checkbox")).isSelected()) {
                    ((JTextField) this.componentList.get("player6Textfield")).setEditable(true);
                    ((JComboBox)  this.componentList.get("player6Combobox")).setEnabled(true);
                } else {    
                    ((JTextField) this.componentList.get("player6Textfield")).setEditable(false);
                    ((JComboBox)  this.componentList.get("player6Combobox")).setEnabled(false);
                }  
                break;
        }
    }
    
    
    
    /**
     *
     */
    public Map<String, Color> getPlayer() {
        ArrayList names  = new ArrayList();
        this.sameName = false;
        Map<String, Color> player = new HashMap<String, Color>();
        
        for (int i = 1; i <= 6; i++) {
            if (((JCheckBox) this.componentList.get("player"+i+"Checkbox")).isSelected()) {
                String selectedColor = (String) ((JComboBox)  this.componentList.get("player"+i+"Combobox")).getSelectedItem();
                Color color;

                if ("Rot".equals(selectedColor)) {
                    color = new Color(255,0,50,255);
                } else if ("Blau".equals(selectedColor)) {
                    color = new Color(50,0,255,255);
                } else if ("Gruen".equals(selectedColor)) {
                    color = new Color(0,255,50,255);
                } else if ("Gelb".equals(selectedColor)) {
                    color = new Color(255,255,0,255);
                } else if ("Cyan".equals(selectedColor)) {
                    color = new Color(0,255,255,255);
                } else if ("Grau".equals(selectedColor)) {
                    color = new Color(75,75,75,255);
                } else {
                    color = Color.WHITE;
                }
                
                String name = (String) ((JTextField) this.componentList.get("player"+i+"Textfield")).getText();

                
                if (names.contains(name)) {
                    this.sameName = true;
                    break;
                } else {
                   names.add(name); 
                }

                player.put(name, color);
            }
        }

        return player;
    }
    
    /**
     *
     */
    public void throwError(String error) {
        JOptionPane.showMessageDialog(null, error, "Folgender Fehler trat auf...",
                JOptionPane.WARNING_MESSAGE);
    }
}
